<template>
  <div>
    <br>
    <el-form :inline = "true">

      <el-form-item label="类别">
        <el-input v-model="searchMap.gategoryId"/>
      </el-form-item>

      <el-form-item label="商品名">
        <el-input v-model="searchMap.name"/>
      </el-form-item>

      <el-form-item label="型号">
        <el-input v-model="searchMap.model"/>
      </el-form-item>

      <el-button type="primary" @click="searchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>

    </el-form>
    <br>

    <el-table
      :data="list"
      border
      style="padding-left: 50;min-width: 100%;">
      <el-table-column
        fixed
        label="操作"
        min-width="10%">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="商品名"
        min-width="10%"/>
      <el-table-column
        prop="gategoryId"
        label="商品类别"
        min-width="10%"/>
      <el-table-column
        prop="mfrs"
        label="制造商"
        min-width="10%"/>
      <el-table-column
        prop="packing"
        label="包装"
        min-width="10%"/>
      <el-table-column
        prop="model"
        label="型号"
        min-width="10%"/>
      <el-table-column
        prop="standard"
        label="规格"
        min-width="10%"/>
      <el-table-column
        prop="color"
        label="颜色"
        min-width="10%"/>
      <el-table-column
        prop="unit"
        label="单位"
        min-width="10%"/>
      <el-table-column
        prop="sellingPrice"
        label="单价"
        min-width="10%"/>
      <el-table-column
        prop="remark"
        label="备注"
        min-width="10%"/>

    </el-table>

    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10,20,30]"
      :page-size="10"
      :total="total"
      layout="total,sizes,prev,pager,next,jumper"
      @szie-change="fetchData"
      @current-change="fetchData"/>

    <el-dialog
      :visible.sync="dialogVisible"
      title="编辑信息"
      width="30%">
      <el-form label-width="100PX">
        <el-form-item label="商品名">
          <el-input v-model="product.name"/>
        </el-form-item>
        <el-form-item label="商品类别">
          <el-input v-model="product.gategoryId"/>
        </el-form-item>
        <el-form-item label="制造商">
          <el-select v-model="product.mfrsId" placeholder="请选择" @change="setProductMfrs()">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.supplier"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="包装">
          <el-input v-model="product.packing"/>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="product.model"/>
        </el-form-item>
        <el-form-item label="规格">
          <el-input v-model="product.standard"/>
        </el-form-item>
        <el-form-item label="颜色">
          <el-input v-model="product.color"/>
        </el-form-item>
        <el-form-item label="单位">
          <el-select v-model="product.unitId" placeholder="请选择" @change="setProductUnit()">
            <el-option
              v-for="item in option"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="单价">
          <el-input v-model="product.sellingPrice"/>
          <el-form-item label="备注">
            <el-input
              :rows="3"
              v-model="product.remark"
              type="textarea"
              placeholder="请输入备注信息"/>
          </el-form-item>
      </el-form-item></el-form>
      <span/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import productApi from '@/api/product'
import supplierApi from '@/api/supplier'
import productUnitApi from '@/api/productUnit'
export default{
  data() {
    return {
      list: [],
      total: 0,			// 总记录数
      currentPage: 1,		// 当前页
      pageSize: 10,		// 每页大小
      searchMap: {},		// 查询条件D
      dialogVisible: false, // 对话框是否可见
      id: '',
      options: [],
      product: {},
      option: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      productApi.findAll(this.currentPage, this.pageSize).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    searchData() {
      productApi.search(this.searchMap, this.currentPage, this.pageSize).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    handleDelete(id) {
      productApi.deleteById(id).then(response => {
        this.$message({
          message: response.message,
          type: 'success'
        })
        if (response.flag) {
          this.fetchData()
        }
      })
    },
    handleSave() {
      productApi.update(this.id, this.product).then(response => {
        this.$message({
          message: response.message,
          type: 'success'
        })
        if (response.flag) {
          this.fetchData()
        }
      })
      this.dialogVisible = false
    },
    handleEdit(id) {
      this.dialogVisible = true
      this.id = id
      supplierApi.findAllByType('供应商').then(response => {
        this.options = response.data
      })
      productUnitApi.findAllUnit().then(response => {
        this.option = response.data
      })
      if (id === '' || id === null) {
        this.product = {}
      } else {
        productApi.findById(this.id).then(response => {
          this.product = response.data
        })
      }
    },
    setProductMfrs() {
      for (let i = 0; i < this.options.length; i++) {
        if (this.options[i].id == this.product.mfrsId) {
          this.product.mfrs = this.options[i].supplier
        }
      }
    },
    setProductUnit() {
      for (let i = 0; i < this.option.length; i++) {
        if (this.option[i].id == this.product.unitId) {
          this.product.unit = this.option[i].name
        }
      }
    }

  }
}
</script>

<style>
</style>
